
'''	
	This file is part of Arendal.
	
	Copyright (C) 2008	Mohamed Hazem <mohamed.hazem@student.guc.edu.eg>

	
	Arendal is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 3 of the License, or
	(at your option) any later version.

	Arendal is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.	See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with Arendal.	If not, see <http://www.gnu.org/licenses/>.
'''


import sys
from PyQt4.QtGui import QWidget, QApplication, QLineEdit, QHBoxLayout, QVBoxLayout, QLabel, QPushButton, QMessageBox, QGridLayout, QDateTimeEdit, QDataWidgetMapper
from PyQt4.QtCore import QDate, SIGNAL
from PyQt4.QtSql import QSqlDatabase, QSqlQuery, QSqlTableModel, QSqlRelationalTableModel, QSqlRelation

class updateProfileForm(QWidget):
		def __init__(self, userID):
			QWidget.__init__(self)

			self.userID = userID

			self.setWindowTitle("User Info")


			self.usernameLabel = QLabel("Username:")
			self.passwordLabel = QLabel("Password:")
			self.emailLabel = QLabel("E-mail:")
			self.addressLabel = QLabel("Address:")
			self.firstNameLabel = QLabel("First Name:")
			self.middleNameLabel = QLabel("Middle Name:")
			self.lastNameLabel = QLabel("Last Name:")
			self.telephoneNumberLabel = QLabel("Telephone Number:")
			self.mobileNumberLabel = QLabel("Mobile Number:")
			self.dateOfBirthLabel = QLabel("Date of Birth:")
			self.countryLabel = QLabel("Country:")

			self.useridLineEdit = QLineEdit()
			self.usernameLineEdit = QLineEdit()
			self.passwordLineEdit = QLineEdit()
			self.passwordLineEdit.setEchoMode(QLineEdit.Password)
			self.emailLineEdit = QLineEdit()
			self.addressLineEdit = QLineEdit()
			self.firstNameLineEdit = QLineEdit()
			self.middleNameLineEdit = QLineEdit()
			self.lastNameLineEdit = QLineEdit()
			self.telephoneNumberLineEdit = QLineEdit()
			self.typeLineEdit = QLineEdit()
			self.mobileNumberLineEdit = QLineEdit()
			self.dateOfBirthLineEdit = QDateTimeEdit()
			self.dateOfBirthLineEdit.setCalendarPopup(True)
			self.dateOfBirthLineEdit.setDisplayFormat("dd-MM-yyyy")
			self.dateOfBirthLineEdit.setMaximumDate(QDate.currentDate())
			self.countryLineEdit = QLineEdit()


			self.updateButton = QPushButton("Update Profile")


			grid = QGridLayout()

			grid.addWidget(self.usernameLabel, 1, 0)
			grid.addWidget(self.usernameLineEdit, 1, 1)
	
			grid.addWidget(self.passwordLabel, 2, 0)
			grid.addWidget(self.passwordLineEdit, 2, 1)

			grid.addWidget(self.emailLabel, 3, 0)
			grid.addWidget(self.emailLineEdit, 3, 1)

			grid.addWidget(self.addressLabel, 4, 0)
			grid.addWidget(self.addressLineEdit, 4, 1)

			grid.addWidget(self.firstNameLabel, 5, 0)
			grid.addWidget(self.firstNameLineEdit, 5, 1)

			grid.addWidget(self.middleNameLabel, 6, 0)
			grid.addWidget(self.middleNameLineEdit, 6, 1)

			grid.addWidget(self.lastNameLabel, 7, 0)
			grid.addWidget(self.lastNameLineEdit, 7, 1)

			grid.addWidget(self.telephoneNumberLabel, 8, 0)
			grid.addWidget(self.telephoneNumberLineEdit, 8, 1)

			grid.addWidget(self.mobileNumberLabel, 9, 0)
			grid.addWidget(self.mobileNumberLineEdit, 9, 1)

			grid.addWidget(self.dateOfBirthLabel, 10, 0)
			grid.addWidget(self.dateOfBirthLineEdit, 10, 1)

			grid.addWidget(self.countryLabel, 11, 0)
			grid.addWidget(self.countryLineEdit, 11, 1)

			grid.addWidget(self.updateButton, 12, 1)

			self.modelUser = QSqlTableModel(self)
			self.connect(self.modelUser, SIGNAL("beforeInsert (QSqlRecord&)"), self.doBeforeInsertion)
			self.modelUser.setTable("User")
			self.modelUser.setFilter("userID=%s" % (self.userID))
			self.modelUser.select()

			self.modelRegisteredUser = QSqlTableModel(self)
			self.connect(self.modelRegisteredUser, SIGNAL("beforeInsert (QSqlRecord&)"), self.doBeforeInsertion)
			self.modelRegisteredUser.setTable("RegisteredUser")
			self.modelRegisteredUser.setFilter("userID=%s" % (self.userID))
			self.modelRegisteredUser.select()

			self.userMapper = QDataWidgetMapper(self)
			self.userMapper.setSubmitPolicy(QDataWidgetMapper.ManualSubmit)
			self.userMapper.setModel(self.modelUser)
			self.userMapper.addMapping(self.useridLineEdit, 0)
			self.userMapper.addMapping(self.usernameLineEdit, 1)
			self.userMapper.addMapping(self.passwordLineEdit, 2)
			self.userMapper.addMapping(self.emailLineEdit, 3)
			self.userMapper.addMapping(self.addressLineEdit, 4)
			self.userMapper.addMapping(self.firstNameLineEdit, 5)
			self.userMapper.addMapping(self.middleNameLineEdit, 6)
			self.userMapper.addMapping(self.lastNameLineEdit, 7)
			self.userMapper.addMapping(self.telephoneNumberLineEdit, 8)
			self.userMapper.addMapping(self.typeLineEdit, 9)

			self.registeredUserMapper = QDataWidgetMapper(self)
			self.registeredUserMapper.setSubmitPolicy(QDataWidgetMapper.ManualSubmit)
			self.registeredUserMapper.setModel(self.modelRegisteredUser)
			self.registeredUserMapper.addMapping(self.useridLineEdit, 0)
			self.registeredUserMapper.addMapping(self.mobileNumberLineEdit, 1)
			self.registeredUserMapper.addMapping(self.dateOfBirthLineEdit, 2)
			self.registeredUserMapper.addMapping(self.countryLineEdit, 3)


			self.userMapper.toLast()
			self.registeredUserMapper.toLast()


			self.connect(self.updateButton, SIGNAL("clicked()"), self.updateUser)

			self.setLayout(grid)

			self.show()

		def updateUser(self):
			self.useridLineEdit.setText(str(self.userID))
			self.typeLineEdit.setText("User")

			if self.usernameLineEdit.text() == '' or self.passwordLineEdit.text() == '':
				QMessageBox.warning(None, "Warning", "Please fill in the required fields")
				return
			else:
				if (self.userMapper.submit() and self.registeredUserMapper.submit()):
					self.hide()


		def doBeforeInsertion(self, sqlRecord):
			for i in range(sqlRecord.count()):
				if sqlRecord.value(i).toString() == '':
					sqlRecord.setNull(i)
				else:
					pass
